#include "Sprite.h"
#include "ShareComponents.h"
#include "FWTexture.h"
#include "FWGraphic.h"

Sprite::Sprite(FWTexture* texture,int width,int height,int count,int spritePerRow,int _rWidth,int _rHeight)
{
	this->_image = texture;
	this->_index = 0;
	this->_spritePerRow = spritePerRow;
	this->_totalFrame = count;
	this->_width = width;
	this->_height = height;	
	_renderWidth = _rWidth;
	_renderHeight = _rHeight;
}

Sprite::Sprite(FWTexture* texture,int width,int height,int count,int spritePerRow)
{
	this->_image = texture;
	this->_index = 0;
	this->_spritePerRow = spritePerRow;
	this->_totalFrame = count;
	this->_width = width;
	this->_height = height;	
	_renderWidth = 0;
	_renderHeight = 0;
}

Sprite::Sprite(Sprite* sprite)
{
	this->_image = sprite->_image;
	this->_index = sprite->_index;
	this->_spritePerRow = sprite->_spritePerRow;
	this->_totalFrame = sprite->_totalFrame;
	this->_width = sprite->_width;
	this->_height = sprite->_height;	
	_renderWidth = sprite->_renderWidth;
	_renderHeight = sprite->_renderHeight;
}


Sprite::~Sprite(void)
{
}

void Sprite::Render(D3DXVECTOR2 position,int width,int height)
{
	RECT frame;
	frame.left = (_index % _spritePerRow) * _width;
	frame.top = (_index / _spritePerRow) * _height;
	frame.right = frame.left + this->_width ;
	frame.bottom = frame.top + this->_height;

	RECT des;
	des.top = position.y;
	des.left = position.x;
	des.right = des.left + width;
	des.bottom = des.top + height;	
	gl_graphic->tDraw(this->_image,frame,des,D3DXVECTOR2(width/2,height/2),D3DCOLOR_XRGB(255,255,255));

}

void Sprite::Render(D3DXVECTOR2 position)
{
	RECT frame;
	frame.left = (_index % _spritePerRow) * _width;
	frame.top = (_index / _spritePerRow) * _height;
	frame.right = frame.left + this->_width ;
	frame.bottom = frame.top + this->_height;

	RECT des;
	des.top = position.y;
	des.left = position.x;
	des.right = des.left + _renderWidth;
	des.bottom = des.top + _renderHeight;	
	gl_graphic->tDraw(this->_image,frame,des,D3DXVECTOR2(_renderWidth/2,_renderHeight/2),D3DCOLOR_XRGB(225,255,255));
}

void Sprite::Next()
{
	_index = (_index + 1) % _totalFrame;
}
void Sprite::Last()
{
	if (_index == 0)_index = _totalFrame - 1;
	else _index = _index-1;
}
int Sprite::GetCurrentIndex()
{
	return _index;
}
void Sprite::Reset()
{
	_index = 0;
}
void Sprite::Reset(int frame)
{
	_index = frame;
}

int Sprite::getRenderWidth()
{
	return _renderWidth;
}

int Sprite::getRenderHeight()
{
	return _renderHeight;
}

D3DXVECTOR2 Sprite::getRenderSize()
{
	return D3DXVECTOR2(_renderWidth,_renderHeight);
}

int Sprite::getCount()
{
	return this->_totalFrame;
}
